//
//  TopUpHistoryViewController.m
//  BaseFrame
//
//  Created by 吕金状 on 2017/8/16.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "TopUpHistoryViewController.h"
#import "TopUpHistoryTableViewCell.h"

@interface TopUpHistoryViewController ()<UITableViewDelegate , UITableViewDataSource>

Strong UITableView *tableView;
Strong NSMutableArray *dataArray; /**<*/

@end

//网络
@implementation TopUpHistoryViewController(NetWorking)

//获取充值列表记录
- (void)getData{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"uId":[UserModel sharedInstanced].userId,
                                 @"orderType":@"0"
                                 };
    [[AFAppDotNetAPIClient sharedClient] POST:@"/LaundryMall/Recharge_Status/displayRechargeInformation.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"responseStatus"] isEqualToString:@"0"]) {
            [self.dataArray removeAllObjects];
            NSArray *array = responseObject[@"obj"];
            [self.dataArray addObjectsFromArray:array];
            [self.tableView reloadData];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
        [self hiddenProgressHUD];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self showError:error];
        [self hiddenProgressHUD];
    }];
}

//删除充值记录
- (void)deleteRechargeInformation:(NSInteger) integer{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"boId":self.dataArray[integer][@"boId"]};
    [[AFAppDotNetAPIClient sharedClient] POST:@"/LaundryMall/Recharge_Status/deleteRechargeInformation.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"responseStatus"] isEqualToString:@"0"]) {
            ShowMessage(responseObject[@"msg"]);
            [self getData];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
        [self hiddenProgressHUD];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self showError:error];
        [self hiddenProgressHUD];
    }];
}

@end

//UI
@implementation TopUpHistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"充值历史";
    [self.view addSubview:self.tableView];
    [self getData];
}

#pragma mark - init

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorInset = UIEdgeInsetsZero;
        if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
            [_tableView setLayoutMargins:UIEdgeInsetsZero];
        }
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = [UIView new];
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.backgroundColor = getColor(@"ffffff");
    }
    return _tableView;
}


#pragma mark - <UITableViewDelegate , UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TopUpHistoryTableViewCell *cell = [TopUpHistoryTableViewCell cellForTableView:tableView];
    cell.model = self.dataArray[indexPath.row];
    __weak __typeof(self)weakSelf = self;
    cell.getDeleteClick = ^{
        UIAlertController *av = [UIAlertController alertControllerWithTitle:@"是否删除" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [weakSelf deleteRechargeInformation:indexPath.row];
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [av addAction:action1];
        [av addAction:action2];
        [self presentViewController:av animated:YES completion:nil];
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 57 / WIDTH_5S_SCALE;
}

#pragma mark - data

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray  = @[
                        ].mutableCopy;
    }
    return _dataArray;
}

@end
